package com.lanou.myim.presenter;


import com.lanou.myim.entity.MsgEty;
import com.lanou.myim.model.ConversationModel;
import com.lanou.myim.model.ConversationModelInterface;
import com.lanou.myim.ui.fragment.ConversationFragmentInterface;

import java.util.List;

/**
 * Created by dllo on 16/3/4.
 */
public class ConversationPresenter implements ConversationPresenterInterface{
    private ConversationModelInterface conversationModel;
    private ConversationFragmentInterface fragmentInterface;
    private static String stickedMsg = null;
    public ConversationPresenter(ConversationFragmentInterface fragmentInterface){
        conversationModel = new ConversationModel();
        this.fragmentInterface = fragmentInterface;
    }

    @Override
    public List<MsgEty> getMsgList() {
        if (stickedMsg == null){
            List<MsgEty> msgEtyList = conversationModel.getNormalMsgEtyList();
            return msgEtyList;
        }else {
            List<MsgEty> msgEtyList = conversationModel.getStickiedMsgEtyList(stickedMsg);
            return msgEtyList;
        }
    }

    @Override
    public void stickMsg(String userName) {
        stickedMsg = userName;
        fragmentInterface.refreshUi(getMsgList());
    }

    @Override
    public void unStickMsg() {
        stickedMsg = null;
        fragmentInterface.refreshUi(getMsgList());
    }

    @Override
    public void insertMsg(MsgEty msgEty) {
          conversationModel.insertMsg(msgEty, new OnCompletedListener() {
              @Override
              public void onCompleted() {
                  fragmentInterface.refreshUi(getMsgList());
              }

              @Override
              public void onError() {
              }
          });
    }

    @Override
    public void deleteMsg(MsgEty msgEty) {
           if (msgEty.getUserName().equals(stickedMsg)){
               stickedMsg = null;
           }
           conversationModel.deleteMsg(msgEty, new OnCompletedListener() {
               @Override
               public void onCompleted() {
                   fragmentInterface.refreshUi(getMsgList());
               }

               @Override
               public void onError() {

               }
           });
    }

    @Override
    public String getStickName() {
     return stickedMsg;
    }


    @Override
    public void searchMsgs(String str) {
        fragmentInterface.refreshUi(conversationModel.searchMsg(str));
    }

}
